package com.wind.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wind.dto.resp.TestClassifyRespDto;
import com.wind.entity.*;
import com.wind.service.*;
import com.wind.vo.ResultData;
import com.wind.vo.ResultDataGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName IndexController
 * @Description Hey, it's compiled! Pack it!
 * @Author Administrator
 * @Date 2021/5/21 23:09
 * @Version 1.0
 * 首页
 */
@Api(tags = "首页相关接口")
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController{

    @Resource
    IArticleService articleService;

    @Resource
    ICarouselService carouselService;

    @Resource
    ICounselorService counselorService;

    @Resource
    ITestClassifyService testClassifyService;

    @Resource
    IAnnouncementService announcementService;

    @Resource
    ITestTopicService testTopicService;

    @Resource
    ITestPaperService testPaperService;
    @ApiOperation("轮播图列表")
    @GetMapping("/carouselList")
    public ResultData<List<Carousel>> carouselList(){
        Wrapper wrapper = new EntityWrapper<Carousel>();
        wrapper.eq("is_active", 1);
        wrapper.eq("location",1);
        wrapper.orderBy("level",false);
        wrapper.last("limit 3");
        List<Carousel> list = carouselService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list,"查询成功");
    }

    @ApiOperation("测试分类")
    @GetMapping("/testClassRecommend")
    public ResultData<List<TestClassify>> testClassRecommend(){
        Wrapper wrapper = new EntityWrapper<TestClassify>();
        wrapper.eq("is_active", 1);
        wrapper.orderBy("sort", false);
        wrapper.last("limit 4");
        List<TestClassify> list = testClassifyService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list, "查询成功");
    }
    @ApiOperation("分类详细")
    @GetMapping("/testClassInfomation/{id}")
    public ResultData<List<TestTopic>> testClassInfomation(@PathVariable Integer id){
        Wrapper wrapper = new EntityWrapper<TestTopic>();
        wrapper.eq("is_active", 1);
        wrapper.eq("text_classify_id",id);
        List<TestTopic> testTopics = testTopicService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(testTopics,"查询成功");
    }
    @ApiOperation("试卷详细")
    @GetMapping("/testInfomation/{id}")
    public ResultData<TestPaper> testInfomation(@PathVariable Integer id){
        Wrapper wrapper = new EntityWrapper<TestPaper>();
        wrapper.eq("is_active", 1);
        wrapper.eq("topic_id",id);
        TestPaper TestPaper = testPaperService.selectOne(wrapper);
        return ResultDataGenerator.genSuccessResult(TestPaper,"查询成功");
    }
    @ApiOperation("所有分类")
    @GetMapping("/testClass")
    public ResultData<List<TestClassifyRespDto>> testClassRecommend1(){
        TestClassifyRespDto testClassifyRespDto = new TestClassifyRespDto();
        testClassifyRespDto.setId(0L);
        return ResultDataGenerator.genSuccessResult( textClassAll(testClassifyRespDto), "查询成功");
    }
    public List<TestClassifyRespDto>  textClassAll(TestClassifyRespDto testClassifyRespDto){
        List<TestClassifyRespDto> resultlist = new ArrayList<>();
        Wrapper wrapper = new EntityWrapper<TestClassify>();
        wrapper.eq("is_active",1);
        wrapper.eq("parent_id",testClassifyRespDto.getId());
        wrapper.orderBy("sort",false);
        List<TestClassify> forList = testClassifyService.selectList(wrapper);
        for (int i = 0 ; i<forList.size();i++) {
            TestClassifyRespDto testClassifyRespDto2 = new TestClassifyRespDto();
            testClassifyRespDto2.setId(forList.get(i).getId());
            testClassifyRespDto2.setParentid(forList.get(i).getParentId());
            testClassifyRespDto2.setName(forList.get(i).getClassifyName());
            testClassifyRespDto2.setLevel(forList.get(i).getLevel());
            resultlist.add(testClassifyRespDto2);
            testClassifyRespDto2.setList(textClassAll(testClassifyRespDto2));
        }
        return resultlist;
    }

    @ApiOperation("心理/职业规划师")
    @GetMapping("/counselor/{type}")
    public ResultData<List<Counselor>> counselorRecommend(@PathVariable("type") Integer type){
        Wrapper wrapper = new EntityWrapper<Counselor>();
        wrapper.eq("is_active", 1);
        wrapper.eq("recommend", 1);
        wrapper.eq("type", type);
        wrapper.orderBy("advisory_count", false);
        List<Counselor> list = counselorService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list, "查询成功");
    }

    @ApiOperation("文章列表")
    @GetMapping("/articleRecommend")
    public ResultData<List<Article>> articleRecommend(){
        Wrapper wrapper = new EntityWrapper<Article>();
        wrapper.eq("is_active", 1);
        wrapper.eq("recommend", 1);
        wrapper.orderBy("create_time", false);
        List<Article> list = articleService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list, "查询成功");
    }

    @ApiOperation("活动公告")
    @GetMapping("/announcementRecommend")
    public ResultData<List<Announcement>> announcementRecommend(){
        Wrapper wrapper = new EntityWrapper<Announcement>();
        wrapper.eq("is_active", 1);
        wrapper.eq("recommend", 1);
        wrapper.orderBy("create_time", false);
        List<Announcement> list = announcementService.selectList(wrapper);
        return ResultDataGenerator.genSuccessResult(list, "查询成功");
    }

    @ApiOperation("搜索")
    @GetMapping("/search")
    public ResultData<Map<String, Object>> search(@RequestParam(value = "key", required = false, defaultValue = "") String key){
        //1 测试套题
        Wrapper testTopicWrapper = new EntityWrapper<TestTopic>();
        testTopicWrapper.eq("is_active", 1);
        if(Objects.nonNull(key) && !"".equals(key)){
            testTopicWrapper.andNew().like("stem", key).or().like("description", key);
        }
        List<TestTopic> testTopicList = testTopicService.selectList(testTopicWrapper);

        //2 咨询师/规划师
        Wrapper counselorWrapper = new EntityWrapper<Counselor>();
        counselorWrapper.eq("is_active", 1);
        if(Objects.nonNull(key) && !"".equals(key)){
            counselorWrapper.andNew().like("name", key).or().like("address", key).or().like("technical_post", key).or().like("adeptdirection", key);
        }
        List<Counselor> list = counselorService.selectList(counselorWrapper);
        //咨询师
        List<Counselor> counselorList = list.stream().filter(counselor -> counselor.getType() == 1).collect(Collectors.toList());
        //规划师
        List<Counselor> plannerList = list.stream().filter(counselor -> counselor.getType() == 2).collect(Collectors.toList());

        //3 咨询文章
        Wrapper articleWrapper = new EntityWrapper<Article>();
        articleWrapper.eq("is_active", 1);
        articleWrapper.like("title", key);
        List<Article> articleList = articleService.selectList(articleWrapper);

        //4 公告
        Wrapper announcementWrapper = new EntityWrapper<Announcement>();
        announcementWrapper.eq("is_active", 1);
        if(Objects.nonNull(key) && !"".equals(key)){
            announcementWrapper.andNew().like("title", key).or().like("content", key);
        }
        List<Announcement> announcementList = announcementService.selectList(announcementWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("testTopicList", testTopicList);
        map.put("counselorList", counselorList);
        map.put("plannerList", plannerList);
        map.put("articleList", articleList);
        map.put("announcementList", announcementList);

        return ResultDataGenerator.genSuccessResult(map, "搜索成功");
    }

}
